package com.bugstack.domain.activity.service.quota.rule.impl;

import com.bugstack.domain.activity.model.entity.ActivityCountEntity;
import com.bugstack.domain.activity.model.entity.ActivityEntity;
import com.bugstack.domain.activity.model.entity.ActivitySkuEntity;
import com.bugstack.domain.activity.model.valobj.ActivityStateVO;
import com.bugstack.domain.activity.service.quota.rule.AbstractActivityChain;
import com.bugstack.types.enums.ResponseCode;
import com.bugstack.types.exception.AppException;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: czb
 * @Description: 活动校验节点
 * @DateTime: 2025/3/21 16:05
 **/
@Component("activity_base_action")
public class ActivityBaseActionChain extends AbstractActivityChain {
    @Override
    public void action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
         //1.校验sku库存数量
        if (activitySkuEntity.getStockCountSurplus() <= 0)
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(), ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        //2.校验活动时间
        Date beginDateTime = activityEntity.getBeginDateTime();
        Date endDateTime = activityEntity.getEndDateTime();
        Date date = new Date();
        if (date.before(beginDateTime) || date.after(endDateTime))
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        //3.校验活动状态
        if (!ActivityStateVO.open.equals(activityEntity.getState()))
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());

        next().action(activitySkuEntity, activityEntity, activityCountEntity);
    }
}
